package pers.cnzdy.tutorial.Chapter5_UI.Section9_ViewPager;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;
import pers.cnzdy.tutorial.R;

public class ViewPagerActivity extends AppCompatActivity {

    private View view1, view2, view3;
    private ViewPager viewPager;  //对应的viewPager
    private List<View> viewList;  //view数组

    PagerAdapter pagerAdapter = new PagerAdapter() {
        String[] titles = new String[] {
                "第一道题",
                "第二道题",
                "第三道题" };

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        viewPager = findViewById(R.id.view_pager);
        LayoutInflater inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.quiz_pager1, null);
        view2 = inflater.inflate(R.layout.quiz_pager2, null);
        view3 = inflater.inflate(R.layout.quiz_pager3, null);

        viewList = new ArrayList<View>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        viewPager.setAdapter(pagerAdapter);

        CircleIndicator indicator = findViewById(R.id.indicator);
        indicator.setViewPager(viewPager);
        pagerAdapter.registerDataSetObserver(indicator.getDataSetObserver());

        viewPager.setPageTransformer(true, new ScalePageTransformer());
        viewPager.setPageTransformer(true, new RotatePageTransformer());

        viewPager.setPageTransformer(true, new GalleryPageTransformer());

    }

    public class ScalePageTransformer implements
            ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.75f;

        @Override
        public void transformPage(View page, float position) {
            // 超出左屏幕
            //Log.d("TAG", "<"+page.hashCode()+", "+position+">");
            if (position < -1.0f) {
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
            } else if (position <= 0.0f) {
                // 向左滑动
                page.setAlpha(1.0f);
                page.setTranslationX(0.0f);
                page.setScaleX(1.0f);
                page.setScaleY(1.0f);
            }
            else if (position <= 1.0f) {
                // 向右滑动
                page.setAlpha(1.0f - position);
                page.setTranslationX(-page.getWidth() * position);
                float scale = MIN_SCALE + (1.0f - MIN_SCALE) * (1.0f - position);
                page.setScaleX(scale);
                page.setScaleY(scale);
            }
            // 超出右屏幕
            else {
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
            }
        }
    }

    public class RotatePageTransformer implements ViewPager.PageTransformer {
        private static final float MAX_ROTATION = 20.0f;

        @Override
        public void transformPage(View page, float position) {
            if (position < -1)
                rotate(page, -MAX_ROTATION);
            else if (position <= 1)
                rotate(page, MAX_ROTATION * position);
            else
                rotate(page, MAX_ROTATION);
        }

        private void rotate(View view, float rotation) {
            view.setPivotX(view.getWidth() * 0.5f);
            view.setPivotY(view.getHeight());
            view.setRotation(rotation);
        }
    }

    public class GalleryPageTransformer implements ViewPager.PageTransformer {
        private static final float MAX_ROTATION = 20.0f;
        private static final float MIN_SCALE = 0.75f;
        private static final float MAX_TRANSLATE = 20.0f;

        @Override
        public void transformPage(View page, float position) {
            if (position < -1) {
                page.setTranslationX(MAX_TRANSLATE);
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
                page.setRotationY(-MAX_ROTATION);
            } else if (position <= 0) {
                page.setTranslationX(-MAX_TRANSLATE * position);
                float scale = MIN_SCALE + (1 - MIN_SCALE) * (1.0f + position);
                page.setScaleX(scale);
                page.setScaleY(scale);
                page.setRotationY(MAX_ROTATION * position);
            } else if (position <= 1) {
                page.setTranslationX(-MAX_TRANSLATE * position);
                float scale = MIN_SCALE + (1 - MIN_SCALE) * (1.0f - position);
                page.setScaleX(scale);
                page.setScaleY(scale);
                page.setRotationY(MAX_ROTATION * position);
            } else {
                page.setTranslationX(-MAX_TRANSLATE);
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
                page.setRotationY(MAX_ROTATION);
            }
        }
    }
}
